<?php

/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/10/10
 * Time: 10:03
 */

function json_success_return($data = [], $msg = '请求数据成功', $code = 200)
{
    echo return_response($msg, $data, $code);
    die;
}

function json_fail_return($msg = '请求数据失败', $data = [], $code = 201)
{
    echo return_response($msg, $data, $code);
    die;
}

if (!function_exists('return_response')) {
    /**
     * @description: 返回响应  @MrLiu
     * @param string $msg
     * @param array $data
     * @param int $code
     * @return string
     */
    function return_response($msg = '', $data = [], $code = 200)
    {
        $arr = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        );

        return json_encode($arr);
    }
}
//im返回
function im_return($data = [])
{
    return json_encode($data);
}

function crypt_en($value)
{
    return \houdunwang\crypt\Crypt::encrypt($value);
}

function crypt_de($value)
{
    return \houdunwang\crypt\Crypt::decrypt($value);
}

function json_to_array($json)
{
    return json_decode($json, true);
}

function from_api()
{
    return (bool) request()->post('token');
}


function get_supplier_type()
{
    #shen 12.24  判断如果是没有登陆的 显示默认账号的商品价格  如果是登录过的 通过token获取父级信息
    $token = input('token', '');
    if (empty($token)) {
        #显示默认账号价格
        $store_id = config('store_id');
    } else {
        #显示对应账号价格
        $token_info = db('token')->where('token', $token)->find();
        if (empty($token_info)) {
            $store_id = config('store_id');
        } else {
            $user_info = db('member')->where('member_id', $token_info['user_id'])->find();
            if (empty($token_info)) {
                $store_id = config('store_id');
            } else {
                if (empty($user_info['invite_store_id'])) {
                    $store_id = config('store_id');
                } else {
                    $store_id = $user_info['invite_store_id'];
                }
            }
        }
    }

    $store_info = db('store')->where('store_id', $store_id)->find();
    return $store_info['supplier_type'];
}


function get_parent_store_id()
{
    #shen 12.24  判断如果是没有登陆的 显示默认店铺id  
    $token = input('token', '');
    if (empty($token)) {
        $store_id = config('store_id');
    } else {
        $token_info = db('token')->where('token', $token)->find();
        if (empty($token_info)) {
            $store_id = config('store_id');
        } else {
            $user_info = db('member')->where('member_id', $token_info['user_id'])->find();
            if (empty($token_info)) {
                $store_id = config('store_id');
            } else {
                if (empty($user_info['invite_store_id'])) {
                    $store_id = config('store_id');
                } else {
                    $store_id = $user_info['invite_store_id'];
                }
            }
        }
    }

    return $store_id;
}


function get_service_parent_store_id($member_id)
{
    if (empty($member_id)) {
        $store_id = config('store_id');
    } else {

        $user_info = db('member')->where('member_id', $member_id)->find();
        if (empty($member_id)) {
            $store_id = config('store_id');
        } else {
            if (empty($user_info['invite_store_id'])) {
                $store_id = config('store_id');
            } else {
                $store_id = $user_info['invite_store_id'];
            }
        }
    }

    return $store_id;
}

function getUserId()
{
    return 1;
}

/**
 * Notes:获取实际安装费用
 * User: shen
 * Date: 2020/4/30
 * Time: 14:27
 * @param int $area_id
 * @param int $install_hour
 */
function get_install_money($area_id = 0, $install_hour = 0)
{
    $config_info = db('config')->where('code', 'default_money')->find();
    $money_info = json_decode($config_info['value'], true);

    $aa = 0;
    $install_money = 0;
    $install_home_money = 0;
    if ($area_id) {
        foreach ($money_info['area_content'] as $k => $v) {
            $ids_array = explode(',', $v['area_id']);
            if (in_array($area_id, $ids_array)) {
                $install_money = $v['install_area_price'];
                $install_home_money = $v['install_home_area_price'];
                $aa = 1;
            }
        }
        if ($aa) {
            #存在
            $money = $install_money * $install_hour + $install_home_money;
        } else {
            #用默认的
            $install_money = (float) $money_info['install_money'];
            $install_home_money = (float) $money_info['install_home_money'];
            $money = $install_money * $install_hour + $install_home_money;
        }
    } else {
        $install_money = (float)$money_info['install_money'];
        $install_home_money = (float) $money_info['install_home_money'];
        $money = $install_money * $install_hour + $install_home_money;
    }
    return $money;
}
